package nz.jdbcwrapper;

import java.sql.SQLWarning;
import java.sql.SQLException;
import java.util.HashSet;
import java.util.Set;

abstract class WarningHolder
{
  final Set warnings = new HashSet();
  final String prefix;
  final long id = Util.id();
  
  WarningHolder(String parentPrefix)
  {
    this.prefix = parentPrefix + "[" + id + "]";
  }

  abstract SQLWarning getProxyWarnings() throws SQLException;

  protected void checkWarnings()
  {
    try
    {
      SQLWarning firstWarning = getProxyWarnings();
      for (SQLWarning warning = firstWarning;
           warning != null;
           warning = warning.getNextWarning()
      )
      {
        if (! warnings.contains(warning))
        {
          warnings.add(warning);
          Util.trace(prefix + " ++++++ " + warning.toString());
        }
      }
      
      for (SQLException excp = firstWarning;
           excp != null;
           excp = excp.getNextException()
      )
      {
        if (! warnings.contains(excp))
        {
          warnings.add(excp);
          Util.trace(prefix + " ===== " + excp.toString());
        }
      }
    }
    catch (SQLException e)
    {
      Util.trace(prefix + "****** Exception during warning checks: " + e);
    }
  }
}
